package wahadlo.controllers;

import java.util.HashSet;
import java.util.Set;

import wahadlo.model.Model;
import wahadlo.model.ModelRecipient;

/**
 * 
 * @author Koziołek
 * 
 */
public class AxisController implements Controller {

	private Model model;
	private Set<ModelRecipient> modelRecipients;
	private Double length;
	private Double weight;
	private Double inclination;
	private DataValidator validator;
	private AnimationImpl animationImpl;
	private String chartType;
	private Double forcing;

	/**
	 * Konstruktor domyślny ustawia walidator.
	 */
	public AxisController() {
		validator = new DataValidatorImpl();
		modelRecipients = new HashSet<ModelRecipient>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see wahadlo.controllers.Controller#bind()
	 */
	public boolean bind() {
		length = (Double) model.getProperties().get("length");
		weight = (Double) model.getProperties().get("weight");
		inclination = (Double) model.getProperties().get("inclination");
		chartType = (String) model.getProperties().get("chartType");
		forcing = (Double) model.getProperties().get("forcing");
		return validate();
	}

	/**
	 * metoda sprawdza poprawność danych. Zwraca <code>true</code> jeżeli dane
	 * są poprawne i <code>false</code> jeżeli są błędne.
	 * 
	 * @return
	 */
	private boolean validate() {
		if (chartType == null)
			return false;
		if (forcing > 1.)
			return false;
		return validator.absoluteGreatThanZero(inclination)
				&& validator.greatThanZero(weight)
				&& validator.greatThanZero(length);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see wahadlo.controllers.Controller#process()
	 */
	public void process() {
		animationImpl = new AnimationImpl(model, modelRecipients);
		Thread thread = new Thread(animationImpl);
		animationImpl.setRunnig(true);
		thread.start();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see wahadlo.controllers.Controller#putModel(wahadlo.model.Model)
	 */
	public void putModel(Model model) {
		this.model = model;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see wahadlo.controllers.Controller#putRecipient(wahadlo.model.ModelRecipient)
	 */
	public void putRecipient(ModelRecipient modelRecipient) {
		this.modelRecipients.add(modelRecipient);
	}

	/**
	 * Zwraca animację którą kontroluje kontroler
	 * 
	 * @return kontrolowana animaja
	 */
	public Animation getAnimation() {
		return animationImpl;
	}

}